package com.example.manage_c20086.api;
import com.alibaba.fastjson.JSONObject;
import com.example.manage_c20086.cutil.cTool;
import com.pojo.Entity.Roles;
import com.pojo.util.Result;
import com.pojo.util.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/roles")
public class rolesApi {
    @Autowired
    private RestTemplate restTemplate;

    private static final String REST_URL_PREFIX="http://ROLES-MANAGEMENT/roles";
    private static final String IDENTIFYING="identifying";
    @GetMapping("/check")
    public Result<?> check(HttpServletRequest httpServletRequest){
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        return restTemplate.postForObject(REST_URL_PREFIX+"/check", request,Result.class);
    }

    @PostMapping("/regiest")
    public Result<?> regiest( Roles role, HttpServletRequest httpServletRequest){
        System.out.println(role);
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        cTool.jsonToMap(role,map);
        map.remove("rBirthday");
        map.add("rBirthday", Tool.getDateToStringSort(role.getRBirthday()));
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        return restTemplate.postForObject(REST_URL_PREFIX+"/regiest", request,Result.class);
    }
    @PostMapping("/delete")
    public Result<?> delete(@RequestParam Integer rID, HttpServletRequest httpServletRequest){
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        map.add("rID",rID);
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        return restTemplate.postForObject(REST_URL_PREFIX+"/delete", request,Result.class);
    }
    @GetMapping("/getlroles")
    public   Result<?>getlroles(@RequestParam Integer Page,
                                @RequestParam Integer Number,
                                Integer uStatus,
                                HttpServletRequest httpServletRequest){
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        map.add("Page",Page);
        map.add("Number",Number);
        map.add("uStatus",uStatus);
        System.out.println(Page+" "+Number+" "+uStatus+ "  roles");
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        return restTemplate.postForObject(REST_URL_PREFIX+"/getlroles", request,Result.class);
    }
    @GetMapping("/getroles")
    public Result<?>getroles(Integer rID, HttpServletRequest httpServletRequest){
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        map.add("rID",rID);
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        return restTemplate.postForObject(REST_URL_PREFIX+"/getroles", request,Result.class);
    }
    @GetMapping("/getmyroles")
    public Result<?>getroles( HttpServletRequest httpServletRequest){
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        return restTemplate.postForObject(REST_URL_PREFIX+"/getmyroles", request,Result.class);
    }

}
